<?php
namespace Home\Controller;

use Utuo\DistributionShopOrderController;
use My\Model\DistributionShopOrderModel;
use My\Model\DeliveryModel;

class DisShopTryOrderController extends DistributionShopOrderController {
	
	/**
	 * 订单结算页
	 */
	public function confirmOrderAction() {
		$orderId = I('orderId', 0, "intval");
		$order = array();
		if ( $orderId ) {
			$order = $this->getPayingOrder( $orderId );
		} else {
			$order = $this->getSaveingOrder();
			session ( "order", $order );
		}
		$this->assign ( "deliveryMethod", DeliveryModel::$METHOD);
		$this->assign ( "orderJSON", json_encode ( $order ) );
		$this->assign ( 'order', $order );
		$this->assign ( "buyUrl", urlencode($this->getCurrentUrl ()) );
		$this->display ( "{$this->modelControllerName}/" . ACTION_NAME."Try" );
	}
		
	/**
	 * 返回欲下单数据
	 * @see \Utuo\DistributionShopOrderController::getItems()
	 */
	protected function getItems() {
		$items = array (
				"atOnce" => 0, // 是否立即购买
				"items" => array ()
		);
		$tryId = I ( "tryId", 0, "intval" ); // 商品id(立即购买时)
		if( $tryId ) { // 处理立即购买的商品
			$try = M("DistributionShopTry")->where(array('uid'=>$this->uid, 'id' => $tryId))->find();
			if ( $try ) {
				$id = $try['productId'];
			} else {
				$this->responseMsg("试用不存在");
			}
			$quantity = 1;
			$attrs = I ( "attrs", null );
			$d = D ( "My/DistributionShopGoods", "Model" );
			// 处理立即购买属性参数,如果提交类型为数组则转为字符串类型(购买链接会再次使用)
			$goods = $d->getUserSubmitGoods ( $id, $attrs );
			if ( !$goods ) {
				$this->responseMsg( $d->getError() );
			}
			// 转化为CartItem(参考购物车结构)
			$businessId = $this->cart->getBusinessId ( $goods ['id'] );
			$items ["atOnce"] = 1;
			$items ["items"] [$goods ['id']] = array (
					'productId' => $goods ['id'],
					'name' => $goods ['name'],
					'support_cod' => $goods ['support_cod'],
					'items' => array (
							$businessId => array (
									'businessId' => $businessId,
									'productPrice' => $goods ['productPrice'],
									'quantity' => $quantity,
									'attrs' => $goods ['selectedAttributes'],
									'pic' => $goods['images'][0] ? $goods['images'][0] : '',
							)
					)
			);
		}
		return $items;
	}
	
	/**
	 * 订单添加前
	 * @see \Utuo\DistributionShopOrderController::_before_add_order()
	 */
	protected function _before_add_order( &$orderData ) {
		$orderData['buy_method'] = DistributionShopOrderModel::BUY_METHOD_TRY;
	}
	
	/**
	 * 订单添加后
	 * @see \Utuo\DistributionShopOrderController::_after_add_order()
	 */
	protected function _after_add_order( &$orderData ) {
		$tryId = I ( "tryId", 0, "intval" ); // 商品id(立即购买时)
		
		/* 设置已攒支持完成 */
		M("DistributionShopTry")->where(array(
			'id' => $tryId,
			'cid' => $this->cid,
		))->save(array(
			"status" => 1, 
			"orderId" => $orderData['id'],
		));
	}
	
	/**
	 * 欲保存订单附加数据
	 * @see \Utuo\DistributionShopOrderController::_after_get_saving_order()
	 */
	protected function _after_get_saving_order( &$savingOrderData ) {
		$tryId = I ( "tryId", 0, "intval" ); // 商品id(立即购买时)
		$savingOrderData['tryId'] = $tryId;
	}
	
}